package com.vpx.server.mapper.system;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.vpx.server.bean.entity.SqlConfig;
import com.vpx.server.bean.entity.SysAttach;
import com.vpx.server.framework.base.BaseMapper;
import com.vpx.server.framework.config.SpringUtils;
import com.vpx.server.service.system.SqlService;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.*;
import org.springframework.aop.framework.AopContext;

import java.util.List;
import java.util.Map;

public interface SqlMapper  extends BaseMapper<SqlConfig> {
    public static final String REPORT_TYPE_KEY = "reportconfig_type";
    /**
     * 执行数据查询分页
     * @param page      查询条件
     * @param sqlScript 脚本
     * @return
     */
    @Select("${sqlScript}")
    Page<Map<String, Object>> reportDataQueryByPage(Page<Map<String, Object>> page,
                                                    @Param("sqlScript") String sqlScript);


    /**
     * 执行数据查询
     * @param sqlScript 脚本
     * @return
     */
    @Select("${sqlScript}")
    List<Map<String, Object>> reportDataQuery(@Param("sqlScript") String sqlScript);

    /**
     * 执行SQL
     * @param sqlScript sql脚本
     * @return
     */
    @Update("${sqlScript}")
    int executeSql(@Param("sqlScript") String sqlScript);

    /**
     * 查询数据量
     * @param sqlScript 脚本
     * @return
     */
    @Select("${sqlScript}")
    long executeCountSql(@Param("sqlScript") String sqlScript);



    /**
     * 根据类型列表删除配置
     * @param typeList
     * @return
     */
    int deleteByTypes(List<String> typeList);

    //    @InsertProvider(type = SqlProvider.class, method = "insertSql")
    @UpdateProvider(type = SqlProvider.class, method = "insertSql")
    int executeOperator(Object param);

    @SelectProvider(type = SqlProvider.class, method = "query")
    List<Map<String,Object>> executeQuery(Object param);
    @SelectProvider(type = SqlProvider.class, method = "queryPageCount")
    Map<String,Object> executeQueryPageCount(Object param);

    @SelectProvider(type = SqlProvider.class, method = "queryPageSqlText")
    Map<String,Object> executeQueryPageSqlText(Object param);
    @SelectProvider(type = SqlProvider.class, method = "queryPage")
    List<Map<String,Object>> executeQueryPage(Object param);
    @Select("SHOW INDEX FROM  test_index")
    List<Map<String,Object>> showIndex();
    class SqlProvider {
        public String insertSql(Object param) {
            SqlConfig config = null;
            Map<String,Object> paramMap = (Map<String,Object>)param;
            if(paramMap.get("queryBySql") == null) {
                config = SpringUtils.getBean(SqlService.class).findByKeyCode((String) paramMap.get(REPORT_TYPE_KEY));
            } else {
                config = new SqlConfig();
                config.setSqlText(paramMap.get("queryBySql").toString());
            }
            System.out.println(config.getSqlText());
            StringBuffer sb = new StringBuffer("");
            sb.append("<script>");
            sb.append(config.getSqlText());
            sb.append(" </script>");

            return sb.toString();
        }
        public String query(Object param) {
            SqlConfig config = null;
            Map<String,Object> paramMap = (Map<String,Object>)param;
            if(paramMap.get("queryBySql") == null) {
                config = SpringUtils.getBean(SqlService.class).findByKeyCode((String) paramMap.get(REPORT_TYPE_KEY));
            } else {
                config = new SqlConfig();
                config.setSqlText(paramMap.get("queryBySql").toString());
            }
            StringBuffer sb = new StringBuffer("");
            sb.append("<script>");
            sb.append(config.getSqlText());
            sb.append(" </script>");
            return sb.toString();
        }
        public String queryPageSqlText(Object param) {
            SqlConfig config = SpringUtils.getBean(SqlService.class).findByKeyCode((String) ((Map<String,Object>)param).get(REPORT_TYPE_KEY));
            StringBuffer sb = new StringBuffer("");
            sb.append("<script>");
            sb.append(config.getQueryPageSqlText());
            sb.append(" </script>");
            return sb.toString();
        }
        public String queryPageCount(Object param) {
            SqlConfig config = SpringUtils.getBean(SqlService.class).findByKeyCode((String) ((Map<String,Object>)param).get(REPORT_TYPE_KEY));
            StringBuffer sb = new StringBuffer("");
            sb.append("<script>");
            sb.append(" select count(1) num from (");
            sb.append(config.getSqlText());
            sb.append(") k");
            sb.append(" </script>");
            return sb.toString();
        }
        public String queryPage(Object param) {
            SqlConfig config = SpringUtils.getBean(SqlService.class).findByKeyCode((String) ((Map<String,Object>)param).get(REPORT_TYPE_KEY));
            StringBuffer sb = new StringBuffer("");
            sb.append("<script>");
            sb.append(" select k.* from (");
            sb.append(config.getSqlText());
            sb.append(") k ");
            String sortField = ((Map<String,Object>) param).get("sortField") == null?"":((Map<String,Object>) param).get("sortField").toString();
            if(StringUtils.isNotBlank(sortField)) {
                String sortOrder = ((Map<String,Object>) param).get("sortOrder") == null?"":((Map<String,Object>) param).get("sortOrder").toString();
                if("descend".equals(sortOrder)) {
                    sb.append(" order by  k."+sortField);
                    sb.append(" desc ");
                } else if("ascend".equals(sortOrder)) {
                    sb.append(" order by  k."+sortField);
                    sb.append(" asc ");
                }
            }
            sb.append(" limit #{offset},#{size}");
            sb.append(" </script>");
            return sb.toString();
        }
    }
}
